package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Pets;
import com.example.utils.ExceptionResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.service.IHealthService;
import com.example.entity.Health;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-10
 */
@CrossOrigin
@RestController
@RequestMapping("/health")
public class HealthController {

        @Resource
        private IHealthService healthService;

        @PostMapping
        public ExceptionResult save(@RequestBody Health health) {
            return ExceptionResult.success(healthService.saveOrUpdate(health));
        }

        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(healthService.removeById(id));
        }

        @GetMapping
        public ExceptionResult findAll() {
            return ExceptionResult.success(healthService.list());
        }

        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(healthService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(healthService.removeByIds(ids));
        }

        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String petsname,
                                        @RequestParam(defaultValue = "") String petsdisease) {
                LambdaQueryWrapper<Health> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(petsname), Health::getPetsname,petsname);
                queryWrapper.like(!"".equals(petsdisease), Health::getPetsdisease,petsdisease);
                return ExceptionResult.success(healthService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
}
